<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="班级">
              <j-dict-select-tag placeholder="请选择班级" v-model="queryParam.classId" 
                dictCode="sk_class,class_name,id,del_flag = 0 order by sort_no asc"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="幼儿姓名">
              <select-student v-model="queryParam.id" :classId="queryParam.classId" placeholder="请输入幼儿姓名"></select-student>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="幼儿性别">
              <j-dict-select-tag placeholder="请选择幼儿性别" v-model="queryParam.sex" dictCode="sex"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="学号">
                <a-input placeholder="请输入学号" v-model="queryParam.studentNo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="证件号码">
                <a-input placeholder="请输入证件号码" v-model="queryParam.idNo"></a-input>
              </a-form-item>
            </a-col> -->
            <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="生日">
                <j-date placeholder="请选择生日" v-model="queryParam.birthday"></j-date>
              </a-form-item>
            </a-col> -->
            <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="家长姓名">
                <a-input placeholder="请输入家长姓名" v-model="queryParam.guardianName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="手机号">
                <a-input placeholder="请输入手机号" v-model="queryParam.phone"></a-input>
              </a-form-item>
            </a-col> -->
            <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="家庭地址">
                <a-input placeholder="请输入家庭地址" v-model="queryParam.address"></a-input>
              </a-form-item>
            </a-col> -->
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="招生年度">
                <a-input placeholder="请输入招生年度" v-model="queryParam.year"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="学生状态">
                <j-dict-select-tag placeholder="请选择学生状态" v-model="queryParam.status" dictCode="student_status"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'management:sk_student:add'">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('儿童管理')" v-has="'management:sk_student:exportXls'">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel" v-has="'management:sk_student:importExcel'">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay" v-has="'management:sk_student:exportXls'">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="deviceId" slot-scope="text">
          <a-tag color="red" v-if="!text">未绑定</a-tag>
          <a-tag color="green" v-else>已绑定</a-tag>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-space>

            <a @click="handleEdit(record)" v-has="'management:sk_student:edit'">编辑</a>
            <a @click="bindBracelet(record)" v-if="!record.deviceId" v-has="'management:sk_student:binding'">绑定手环</a>
            <a @click="unBindBracelet(record)" v-else v-has="'management:sk_student:binding'">解绑手环</a>
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item v-has="'management:sk_student:info'">
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item v-has="'management:sk_student:delete'">
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </a-space>
        </span>

      </a-table>
    </div>

    <sk-student-modal ref="modalForm" @ok="modalFormOk"></sk-student-modal>
    <sk-bracelet ref="braceletModal" @ok="modalFormOk"></sk-bracelet>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import SkStudentModal from './modules/SkStudentModal'
  import SelectStudent from '@/components/skComponent/SelectStudent'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import SkBracelet from "./modules/SkBracelet.vue"
  import { getAction } from "@/api/manage.js";
  export default {
    name: 'SkStudentList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      SkStudentModal,
      SelectStudent,
      SkBracelet
    },
    data () {
      return {
        description: '幼儿管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'学号',
            align:"center",
            dataIndex: 'studentNo'
          },
          {
            title:'姓名',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'出生日期',
            align:"center",
            dataIndex: 'birthday',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'性别',
            align:"center",
            dataIndex: 'sex_dictText'
          },
          {
            title:'班级',
            align:"center",
            dataIndex: 'classId_dictText'
          },
          {
            title:'招生年度',
            align:"center",
            dataIndex: 'year'
          },
          {
            title:'家长姓名',
            align:"center",
            dataIndex: 'guardianName'
          },
          {
            title:'联系方式',
            align:"center",
            dataIndex: 'phone'
          },
          {
            title:'学生状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title:'手环绑定',
            align:"center",
            dataIndex: 'deviceId',
            scopedSlots: { customRender: 'deviceId' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/management/skStudent/list",
          delete: "/management/skStudent/delete",
          deleteBatch: "/management/skStudent/deleteBatch",
          exportXlsUrl: "/management/skStudent/exportXls",
          importExcelUrl: "management/skStudent/importExcel",
          
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      bindBracelet(record){
        this.$refs.braceletModal.edit(record);
      },
      unBindBracelet(record){
        let httpurl = '/management/skStudent/bindSwatch';
        let dataForm = {
          deviceId:"",
          studentId:record.id
        }
        getAction(httpurl,dataForm).then((res)=>{
          if(res.success){
            this.$message.success("解绑成功！ ");
            this.loadData();
          }else{
            this.$message.warning(res.message);
          }
        }).finally(() => {
        })
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'idType',text:'证件类型',dictCode:'certificate_type'})
        fieldList.push({type:'string',value:'idNo',text:'证件号码',dictCode:''})
        fieldList.push({type:'string',value:'studentNo',text:'学号',dictCode:''})
        fieldList.push({type:'string',value:'name',text:'姓名',dictCode:''})
        fieldList.push({type:'date',value:'birthday',text:'生日'})
        fieldList.push({type:'int',value:'sex',text:'性别',dictCode:'sex'})
        fieldList.push({type:'string',value:'phone',text:'手机号',dictCode:''})
        fieldList.push({type:'string',value:'address',text:'家庭地址',dictCode:''})
        fieldList.push({type:'string',value:'photo',text:'照片',dictCode:''})
        fieldList.push({type:'string',value:'classId',text:'班级',dictCode:"sk_class,class_name,id"})
        fieldList.push({type:'string',value:'year',text:'招生年度',dictCode:''})
        fieldList.push({type:'string',value:'guardianName',text:'家长姓名',dictCode:''})
        fieldList.push({type:'string',value:'guardianIdType',text:'家长证件类型',dictCode:'certificate_type'})
        fieldList.push({type:'string',value:'guardianIdNo',text:'家长证件号码',dictCode:''})
        fieldList.push({type:'int',value:'status',text:'学生状态',dictCode:'student_status'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>